package com.zxj.netty.client;

import org.apache.commons.lang.StringUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

/**
 * @Description:
 * @Author:zhangxiaojun
 * @Date:Created in 18:08 2018/11/7
 */
public class SystemAliasParser {

    private static final String SYSTEM_ALIAS_DIR = "META-INF/service-alias";


    public void parse() {
        Set<String> classNames = new HashSet<>();
        ClassLoader loader = this.getClass().getClassLoader();

        try {
            Enumeration<URL> list = loader.getResources(SYSTEM_ALIAS_DIR);

            while (list.hasMoreElements()) {
                URL item = list.nextElement();
                URLConnection connection = item.openConnection();
                connection.setUseCaches(false);

                InputStream is = connection.getInputStream();
                try {
                    InputStreamReader streamReader = new InputStreamReader(is, "UTF-8");
                    BufferedReader nameReader = new BufferedReader(streamReader);
                    String className;
                    while (StringUtils.isNotBlank((className = nameReader.readLine()))) {
                        classNames.add(className);
                    }
                } finally {
                    is.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }

        for (String name : classNames) {

        }
    }
}
